package com.ruoyi.web.controller.villagers;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.villagers.domain.VigCard;
import com.ruoyi.villagers.service.IVigCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/villagers/card")
public class VigCardController extends BaseController {

    @Autowired
    private IVigCardService iVigCardService;

    /**
     * 获取身份证列表信息
     */

    @PreAuthorize("@ss.hasPermi('villagers:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(VigCard card){
        startPage();
        List<VigCard> list = iVigCardService.selectCardList(card);
        return getDataTable(list);
    }

    /**
     * 新增身份证信息
     */
    @PreAuthorize("@ss.hasPermi('villagers:card:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody VigCard card)
    {
        if (UserConstants.NOT_UNIQUE.equals(iVigCardService.checkCardIdUnique(card)))
        {
            return AjaxResult.error("新增身份证信息'" + card.getCardName() + "'失败，岗位名称已存在");
        }
        card.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iVigCardService.insertCard(card));
    }

    /**
     * 修改身份证号
     */
    @PreAuthorize("@ss.hasPermi('villagers:card:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody VigCard card)
    {
        if (UserConstants.NOT_UNIQUE.equals(iVigCardService.checkCardIdUnique(card)))
        {
            return AjaxResult.error("修改身份证'" + card.getCardId() + "'失败，身份证号已存在");
        }

        card.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iVigCardService.updateCard(card));
    }

    /**
     * 根据身份证编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('villagers:card:query')")
    @GetMapping(value = "/{cardId}")
    public AjaxResult getInfo(@PathVariable String cardId)
    {
        return AjaxResult.success(iVigCardService.selectCardById(cardId));
    }

    /**
     * 删除身份证
     */
    @PreAuthorize("@ss.hasPermi('villagers:card:remove')")
    @Log(title = "身份证管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cardIds}")
    public AjaxResult remove(@PathVariable String[] cardIds)
    {
        return toAjax(iVigCardService.deleteCardByIds(cardIds));
    }

    /**
     * 获取身份证选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<VigCard> cards = iVigCardService.selectCardAll();
        return AjaxResult.success(cards);
    }




}
